package io.renren.modules.evaluate.controller;

import io.renren.common.utils.R;
import io.renren.modules.evaluate.dao.IndexSystemDao;
import io.renren.modules.evaluate.entity.IndexSystemEntity;
import io.renren.modules.evaluate.entity.IndexSystemMidEntity;
import io.renren.modules.evaluate.entity.Sys;
import io.renren.modules.evaluate.entity.TreeNode;
import io.renren.modules.evaluate.service.IndexSystemService;
import io.renren.modules.evaluate.util.TreeNodeUtil;
import io.renren.modules.sys.controller.SysLoginController;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.*;

@RestController
public class IndexSystemController {

    @Autowired
    IndexSystemService indexSystemService;

    @Autowired
    SysLoginController sysLoginController;

    @Autowired
    IndexSystemDao indexSystemDao;

/**
 * 指标体系的展示
 *
 * */
    /**
     * 向前端返回某一个指标体系的全部信息
     */
   @ApiOperation("queryByIndexSysName")
    @GetMapping("/indexSysMes")
    public R queryByIndexSysName(@RequestParam Integer id){
        // 获取指标体系的详细信息
        IndexSystemEntity indexSysMes = indexSystemService.getById(id);
        if(indexSysMes == null){
            return R.error(-1, "该指标体系不存在");
        }
        // 获取指标体系对应的多个指标
        List<IndexSystemMidEntity> idxSysMidEnts = indexSystemService.getIdxSysMidById(id);

        List<TreeNode> treeNodeList = new LinkedList<TreeNode>();
        for(IndexSystemMidEntity mes : idxSysMidEnts){
            TreeNode tNode = new TreeNode();
            tNode.setId(mes.getCurrent_id().toString());    // 当前指标的位置id
            tNode.setPid(mes.getParent_id().toString());    // 当前指标的父节点位置id
            tNode.setLabel(mes.getNormMes().getMes());     // 当前指标的名称
            tNode.setIndexId(mes.getIndex_id());            // 当前指标id
            treeNodeList.add(tNode);
        }
       /*System.out.println(treeNodeList);*/
        List<TreeNode> treeNodeList2 = TreeNodeUtil.transformData(treeNodeList,"0");
       System.out.println(treeNodeList2);
        indexSysMes.setTreeNodes(treeNodeList2);
        return R.ok().put("indexSysMes", indexSysMes);

    }



        @ApiOperation("根据userid得到所有指标体系")
        @GetMapping("/index/system/findbyid")
        public List<IndexSystemEntity> findbyid(){return indexSystemService.findAllByUserId(sysLoginController.userid);}

        @ApiOperation("保存指标体系")
        @PostMapping ("/index/system/get")
        @ResponseBody
        public Map<String,Object> iste(@RequestBody ArrayList<Sys> sys,String name){
            Map<String,Object> resultMap = new HashMap<>();
            IndexSystemEntity indexsystem = new IndexSystemEntity();
            IndexSystemEntity ise = indexSystemService.selByName(name);
            if (ise!=null){
                indexSystemService.delIndexSystemByName(name);
                indexSystemService.delIndexSystemMidById(ise.getId());
            }

            System.out.println(sys.toString());


            /*---------------保存指标体系表------------------*/
            indexsystem.setIndex_system_name(name);
            indexsystem.setStatus("0");
            indexsystem.setEstablished_people_id(sysLoginController.userid);
            indexSystemService.insertIndexSystem(indexsystem);
            /*--------------保存指标体系中间表------------------*/
            for (int i=0;i<sys.size();i++){
                IndexSystemMidEntity indexsystemmid = new IndexSystemMidEntity();
                indexsystemmid.setIndex_system_id(indexsystem.getId());
                indexsystemmid.setCurrent_id(Integer.parseInt(sys.get(i).getId()));
                indexsystemmid.setParent_id(Integer.parseInt(sys.get(i).getPid()));
                indexsystemmid.setIndex_id(Integer.parseInt(sys.get(i).getIndexid()));
                indexSystemService.insertIndexSystemMid(indexsystemmid);
            }
            resultMap.put("code", 0);
            resultMap.put("msg", "success");
            return resultMap;
        }

    /**
     * 删除指标体系
     */
    @ApiOperation("deleteById")
    @DeleteMapping("/delete")
//    @RequiresPermissions("generator:indexsystem:delete")
    public R delete(@RequestBody Integer id){
        Integer rows = indexSystemService.deleteByIdxSysId(id);
        return R.ok();
    }

    @ApiOperation("得到指标体系")
    @GetMapping("/getBySYSid")
    public List<IndexSystemMidEntity> fidaall(Integer id){
        return indexSystemDao.getBySysId(id);
    }


}
